package api

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/base"
	"cgs-server/server/cgs/app/models"
	"cgs-server/server/cgs/app/models/dto"
	v1 "cgs-server/server/cgs/app/models/v1"
	"cgs-server/server/cgs/app/service"
	"cgs-server/server/cgs/app/store"
	"go.mongodb.org/mongo-driver/bson"
	"net/http"
)

type WorksheetShortCutHandler struct {
	// srv service
	srv service.Service
	// store
	store store.Factory
}

func NewWorksheetShortCutHandler(store store.Factory) *WorksheetShortCutHandler {
	return &WorksheetShortCutHandler{
		srv:   service.NewService(store),
		store: store,
	}
}

func (wg *WorksheetShortCutHandler) Get(w http.ResponseWriter, r *http.Request) {
	sheetId := r.URL.Query().Get("sheetId")
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	group, err := wg.srv.WorksheetShortCut().Detail(user, sheetId)
	list := group.(*[]bson.M)
	for _, v := range *list {
		sheetId := v["sheetId"].(string)
		worksheet, err := wg.srv.WorksheetInfo().Get(sheetId, user)
		if err == nil {
			v["label"] = worksheet.Name
		}

	}
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(group))
}

func (wg *WorksheetShortCutHandler) Delete(w http.ResponseWriter, r *http.Request) {
	sheetId := r.FormValue("sheetId")
	if sheetId == "" {
		_, _ = helper.WriteJSON(w, server.Fail("sheet id is required"))
		return
	}
	url := r.FormValue("url")
	linkType := r.FormValue("linkType")
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	err = wg.srv.WorksheetShortCut().Delete(sheetId, url, linkType, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}

func (wg *WorksheetShortCutHandler) Save(w http.ResponseWriter, r *http.Request) {
	var entity models.WorksheetShortCut
	err := helper.ReadRequestBody(&entity, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	//check duplicat
	options := v1.ListOptions{}
	options.AddFilter("sheetId", entity.SheetId)
	options.AddFilter("url", entity.Url)
	options.AddFilter("linkType", entity.LinkType)
	findList, _ := wg.store.WorksheetSortCut().List(&models.WorksheetShortCut{}, user, options)
	if len(*findList) > 0 {
		_, _ = helper.WriteJSON(w, server.Fail("Row Exists"))
		return
	}

	err = wg.srv.WorksheetShortCut().Save(&entity, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(bson.M{"id": entity.GetId()}))
}

func (wg *WorksheetShortCutHandler) Update(w http.ResponseWriter, r *http.Request) {
	var wsGroup dto.WorksheetShortCut
	err := helper.ReadRequestBody(&wsGroup, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.WorksheetShortCut().Update(&wsGroup, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}
